//
//  LXLUserAccount.swift
//  Weibo-YQY-test
//
//  Created by Lang on 10/15/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import UIKit

class LXLUserAccount: NSObject,NSCoding {
    
    // 过期时间
    var expiresDate: NSDate?
    
//    "access_token" = "2.00_mmEgCKr_1DDa4b4a2dcc83NIA3C";
//    "expires_in" = 157679999;
//    "remind_in" = 157679999;
//    uid = 2454012595;
    // 访问令牌
    var access_token: String?
    // 过期时间，秒数是一个基本数据类型，系统默认不会分配存储空间
    var expires_in: NSTimeInterval = 0 {
        didSet{
            // 1.取到当前的时间
            let nowDate = NSDate()
            // 2.在当前的时间上面加上expires_in秒，会产生一个心的时间
            expiresDate = nowDate.dateByAddingTimeInterval(expires_in)
        }
    }
    
    var remind_in: String?
    // 用户的 uid
    var uid: String?
    //用户的高清头像
    var avatar_large: String?
    //用户的昵称
    var name: String?
    
    
    
    
    
    
    
    
    
    
    
    
    init(dictionary: [String: AnyObject]) {
        super.init()
        
        //字典转模型，kvc
        setValuesForKeysWithDictionary(dictionary)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String{
        let keys = ["access_token","expiresDate","remind_in","uid", "name", "avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
   
    
    
    
    // 1.拿到存储的路径
    // static 来修饰的话，代码这个属性跟随类的的存在而存在
    static let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.archive")
    
    
    // 保存当前账户对象
    func saveAccount(){
        // 2.归档
        NSKeyedArchiver.archiveRootObject(self, toFile: LXLUserAccount.path)
    }
    
    class func loadUserAccount() -> LXLUserAccount? {
        //解档，获取数据
        let account = NSKeyedUnarchiver.unarchiveObjectWithFile(self.path) as? LXLUserAccount
        //获取帐号的时候，需要判断 accessToken 是否过期
        
        if let acc = account{
            // 拿着过期时间与当前时间进行对比
            // 如果过期时间大于当前时间，代表没有过期。否则代表过期
            if acc.expiresDate?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return account
            }
        }
        return nil
    }
    
    
    //告诉系统怎么去归档
    func encodeWithCoder(encoder: NSCoder){
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(name, forKey: "name")
    }
    //告诉系统怎么去解档
    required init? (coder aDecoder: NSCoder){
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
    }
    
    
    
    // 判断用户是否登录
    class func isLogin(loginSuccess loginSuccess: UIViewController, noLogin: UIViewController = UIViewController()){
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let window = delegate.window
        
        
        let account = loadUserAccount()
        
        if let _ = account {
            // 如果登录--> 进入到欢迎页
            window?.rootViewController = loginSuccess
        }else{
            window?.rootViewController = LXLNavigationController(rootViewController: noLogin)
        }
    }
    
    
    
    
    
}
